package com.nineclock.system.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.constant.SMSConstant;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.sms.AliyunSmsUtils;
import com.nineclock.common.utils.PhoneNumCheckUtils;
import com.nineclock.system.mapper.SysUserMapper;
import com.nineclock.system.pojo.SysUser;
import com.nineclock.system.service.SMSService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
@Slf4j
public class SMSServiceImpl implements SMSService {

    @Autowired
    RedisTemplate<String, String> redisTemplate;

    @Autowired
    AliyunSmsUtils aliyunSmsUtils;

    @Autowired
    SysUserMapper userMapper;

    /**
     *
     * @param mobile
     * @param type
     */
    @Override
    public void sendCode(String mobile, String type) {
        if(mobile == null || type == null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        //1.验证手机号是否可以使用
        if(! PhoneNumCheckUtils.isPhoneLegal(mobile)){
            throw new NcException(ResponseEnum.INVALID_PHONE_NUMBER);
        }
        //根据手机号查询用户信息
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getMobile, mobile);
        SysUser user = userMapper.selectOne(wrapper);


        //如果是注册， 手机不能在数据库存在
        if("2".equals(type)){
            if(user != null){
                throw new NcException(ResponseEnum.USER_MOBILE_EXISTS);
            }
        }
        //否则 是相反的
        else{
            if(user == null){
                throw new NcException(ResponseEnum.USER_NOT_REGISTER);
            }
        }

        //2.生成验证码
        String code = RandomUtil.randomNumbers(6);

        //3.发送验证码到手机
//        aliyunSmsUtils.sendSMS(mobile, code);
        log.info("验证码：" + code);

        //4.存储验证码到redis(5分钟)

        String key = getPrefix(type) + mobile;

        redisTemplate.boundValueOps(key).set(code, 5, TimeUnit.MINUTES);

    }

    @Override
    public Boolean verify(String mobile, String checkcode, String type) {
        if(mobile == null || checkcode == null || type == null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        //redis 的key
        String key = getPrefix(type) + mobile;
        //获取redis的验证码
        String codeInRedis = redisTemplate.boundValueOps(key).get();

        if(! checkcode.equals(codeInRedis)){
            throw new NcException(ResponseEnum.CODE_IMAGE_ERROR);
        }
        //删除redis 中的数据
        redisTemplate.delete(key);

        return true;
    }


    public String getPrefix(String type){
        switch (type){
            case SMSConstant.SMS_TYPE_LOGIN : return SMSConstant.SMS_LOGIN_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_REGISTER : return SMSConstant.SMS_REGISTER_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_CHANGE_MANAGER:  return SMSConstant.SMS_CHANGE_MANAGER_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_RESET_PASSWORD : return SMSConstant.SMS_RESET_KEY_PREFIX;
            default: return null;
        }
    }
}
